const express = require('express');
const router = express.Router();
const multer = require('multer');
const Auth=require('../middleware/auth');

const stroage = multer.diskStorage({
    destination: (req, file, cb) => {
        cb(null, 'uploads/')
    },
    filename: (req, file, cb) => {
        cb(null, Date.now() +'_'+file.originalname)
    }
})
const fileFilter = (req, file, cb) => {
    if (file.mimetype === 'image/jpeg' || file.mimetype === 'image/png') {//文件类型过滤，只接收jpg或png格式图片
        cb(null, true)
    } else {
        cb(null, false)
    }
}
const upload = multer({
    storage: stroage,
    limits: {
        fileSize: 1024 * 1024 * 5
    },
    fileFilter: fileFilter
})

const productController = require('../controllers/product')

router.get('/',productController.product_get_all);//获取所有产品
router.get('/search',productController.product_get_search);//搜索产品
router.post('/',Auth, upload.single('proImg'), productController.product_post);//提交产品信息
router.get('/:pid', productController.product_getById);//获取单个产品信息
router.patch('/:pid', productController.product_patchById);//更新产品信息
router.delete('/batch', productController.product_deleteMany);//批量删除
router.delete('/:pid', productController.product_deleteById);//删除单个产品

module.exports = router;